#include "CheckersGame.h"
#include <iostream>

CheckersGame::CheckersGame()
{
    //ctor
    selectedRow = -1;
    selectedCol = -1;
    isPieceSelected = false;
    isJumpAgain = false;
    mCheckersBoard = new CheckersBoard();
    gameType = selectGameType();
    doNewGame(gameType);
}

CheckersGame::~CheckersGame()
{
    //dtor
}

int CheckersGame::selectGameType()
{
    //console implementation
    int tGameType;
    cout<< "game type: ";
    cin >> tGameType;
    gameType = tGameType;
    cout<<"gameType: "<<gameType<<endl;
    if (gameType < 1 || gameType > 2) {
        currentTurn = HUMAN;
        return HUMAN_HUMAN; //default is human vs human
    }
    gameType = tGameType;
    currentTurn = HUMAN;
    return tGameType;
}

void CheckersGame::doNewGame(int pGameType)
{
    if (pGameType == HUMAN_COMPUTER) {
        mCheckersComputer = new CheckersComputer(mCheckersBoard);
    }
    mCheckersBoard->initializeBoard();
    currentPlayer = BLACK;
    mCheckersBoard->calculateLegalMoves(currentPlayer);
    switch (pGameType)
    {
        case HUMAN_HUMAN : {
            player1 = HUMAN;
            player2 = HUMAN;
            break;
        }
        case HUMAN_COMPUTER : {
            player1 = HUMAN;
            player2 = COMPUTER;
            break;
        }
        case COMPUTER_COMPUTER : {
            player1 = COMPUTER;
            player2 = COMPUTER;
            break;
        }
    }
}

void CheckersGame::doClickSquare(int pRow, int pCol)
{
    int i;
    for (i=0;i<(int)mCheckersBoard->getLegalMoves().size();i++)
    {
        if (pRow == mCheckersBoard->getLegalMoves().at(i)->getFromRow() && pCol == mCheckersBoard->getLegalMoves().at(i)->getFromCol()) {
            setSelectedRow(pRow);
            setSelectedCol(pCol);
            isPieceSelected = true;
        }
    }
    //setelah iterasi
    if (!isPieceSelected) {
        //biji yang dipilih gak legal
        isPieceSelected = false;
        selectedRow = -1;
        selectedCol = -1;
    } else {
        cout << "-------------" << endl;
        for (i=0;i<(int)mCheckersBoard->getLegalMoves().size();i++)
        {
            if (selectedRow == mCheckersBoard->getLegalMoves().at(i)->getFromRow() &&
                selectedCol == mCheckersBoard->getLegalMoves().at(i)->getFromCol() &&
                pRow == mCheckersBoard->getLegalMoves().at(i)->getToRow() &&
                pCol == mCheckersBoard->getLegalMoves().at(i)->getToCol()) {
                isPieceSelected = false;
                isJumpAgain = false;
                doMovePiece(mCheckersBoard->getLegalMoves().at(i));
            }
        }
    }
}

void CheckersGame::doMovePiece(CheckersMove* pMove)
{
    mCheckersBoard->movePiece(pMove);
    if (pMove->isJump()) {
        mCheckersBoard->calculateLegalJumps(currentPlayer,pMove->getToRow(),pMove->getToCol());
        if (mCheckersBoard->getLegalMoves().size() > 0) {
            selectedRow = pMove->getFromRow();
            selectedCol = pMove->getFromCol();
            isPieceSelected = true;
            cout << "lompat lagi :D" << endl;
            isJumpAgain = true;
            mCheckersBoard->calculateLegalMoves(currentPlayer);
        }
    }
    if (!isJumpAgain) {
        if (currentPlayer == RED) {
            std::cout << "change player to black" << endl;
            currentPlayer = BLACK;
            mCheckersBoard->calculateLegalMoves(currentPlayer);
        } else {
            std::cout << "change player to red" << endl;
            currentPlayer = RED;
            mCheckersBoard->calculateLegalMoves(currentPlayer);
        }
    }
    cout << "gameType: "<< gameType << endl;
    if (gameType != HUMAN_HUMAN) {
        if (currentTurn == HUMAN) {
            currentTurn = COMPUTER;
            mCheckersComputer->doComputerMove();
            if (currentPlayer == RED) {
                std::cout << "change player to black" << endl;
                currentPlayer = BLACK;
                mCheckersBoard->calculateLegalMoves(currentPlayer);
            } else {
                std::cout << "change player to red" << endl;
                currentPlayer = RED;
                mCheckersBoard->calculateLegalMoves(currentPlayer);
            }
            currentTurn = HUMAN;
        } else {
            currentTurn = HUMAN;
        }
    }
}
